/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package chequeTeatinos.beans;

import chequeTeatinos.ejb.EmpresaFacade;
import chequeTeatinos.entities.Empresa;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import org.primefaces.model.UploadedFile;
import org.apache.commons.io.FileUtils;
import org.primefaces.event.FileUploadEvent;

/**
 *
 * @author escabia
 */
@ManagedBean
@SessionScoped
public class AddEmpresaBean {

    @EJB
    private EmpresaFacade empresaFacade;
    
    
    private String nombre;
    private String direccion;
    private UploadedFile uploadedFile;
    
    /**
     * @return 
     */
    private int idnuevaoferta(){
        int idnueva = 0;
        List<Empresa> o = empresaFacade.findAll();
        
        for(int i=0; i<o.size(); i++){
            if(idnueva < o.get(i).getId())
                idnueva = o.get(i).getId();
        }
        
        return idnueva+1;
    }
    
    
    /**
     * Creates a new instance of AddEmpresaBean
     */
    public AddEmpresaBean() {
    }

    
    public void crearEmpresa() throws IOException{
        FacesMessage msg = new FacesMessage("Empresa creada");
        FacesContext.getCurrentInstance().addMessage(null, msg);

        InputStream inputStr = null;
        try {
            inputStr = uploadedFile.getInputstream();
        } catch (IOException e) {
            //log error
        }

        //create destination File
        
        String absoluteDiskPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/");
        String path=absoluteDiskPath.substring(0,absoluteDiskPath.indexOf("build"));
        String destPath = path+"web/images/"+uploadedFile.getFileName();
        System.out.println(destPath);
        
        
        File destFile = new File(destPath);
        //use org.apache.commons.io.FileUtils to copy the File
        try {                    
            FileUtils.copyInputStreamToFile(inputStr, destFile);
        } catch (IOException e) {
            //log error
        }
        
        Empresa e = new Empresa();
        e.setId(idnuevaoferta());//NO ES ASI
        e.setNombre(this.nombre);
        e.setDireccion(this.direccion);
        e.setImagen("/images/"+uploadedFile.getFileName());
        empresaFacade.create(e);
    }    
    
    
    public EmpresaFacade getEmpresaFacade() {
        return empresaFacade;
    }

    public void setEmpresaFacade(EmpresaFacade empresaFacade) {
        this.empresaFacade = empresaFacade;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public String getDireccion() {
        return direccion;
    }

    public void setDireccion(String direccion) {
        this.direccion = direccion;
    }

    public UploadedFile getUploadedFile() {
        return uploadedFile;
    }

    public void setUploadedFile(UploadedFile uploadedFile) {
        this.uploadedFile = uploadedFile;
    }    
    
    public void handleFileUpload(FileUploadEvent event) {
        FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
}
